package com.java2104.mapper;

import com.java2104.entity.Website;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.List;

@Repository
public interface WebsiteMapper {
//    @Select("select * from websites where name like '%${param1}%'")
    //这个方法需要映射一个动态SQL，在XML Mapper中完成映射
    List<Website> selectList(String name, String country);
    //查询所有的国家（去重）
    @Select("select distinct country from websites")
    List<String> selectCountryList();

    //添加一个站点
    @Insert("insert into websites value(null, #{name}, #{url}, #{alexa}, #{country})")
    int insert(Website website);

    //根据ID删除站点
    @Delete("delete from websites where id=#{param1}")
    int deleteByPrimaryKey(int id);

    //返回某个站点在某天的访问记录，返回1说明已经被访问过，返回0说明还没有访问过
    @Select("select count(*) from access_log where site_id=#{param1} and date=#{param2}")
    int selectCount(int siteId, Date today);

    //添加一条访问记录
    @Insert("insert into access_log value(null, #{param1}, 1, #{param2})")
    int insertAccessLog(int siteId, Date today);

    //更新访问次数（+1）
    @Update("update access_log set count=count+1 where site_id=#{param1} and date=#{param2}")
    int updateAccessCount(int siteId, Date today);
}
